package com.wxl.common.pop

import android.content.Context
import android.os.Build
import com.lxj.xpopup.core.BasePopupView
import com.wxl.common.R
import kotlinx.android.synthetic.main.pop_select_time.view.*

/**
 * create file time : 2021/4/12
 * create user : wxl
 * subscribe :
 */
class TimePickerPop : BasePopupView {

    constructor(context: Context) : super(context)

    private lateinit var callback: OnSubmitTimeCallback

    override fun getPopupLayoutId(): Int {
        return R.layout.pop_select_time
    }

    override fun onCreate() {
        super.onCreate()
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            datePickerView.setOnDateChangedListener { view, year, monthOfYear, dayOfMonth ->

            }
        }
        timePickerView.setOnTimeChangedListener { view, hourOfDay, minute ->

        }

        timePickerView.setIs24HourView(true)
        timePickerCancel.setOnClickListener {
            dismiss()
        }

        timePickerSubmit.setOnClickListener {
            dismiss()
            var h =
                if ((timePickerView.currentHour) > 10) (timePickerView.currentHour).toString() else "0" + (timePickerView.currentHour)
            var m =
                if (timePickerView.currentMinute > 10) timePickerView.currentMinute.toString() else "0" + timePickerView.currentMinute
            var month =
                if ((datePickerView.month + 1) > 10) (datePickerView.month + 1).toString() else "0" + (datePickerView.month + 1)
            var day =
                if (datePickerView.dayOfMonth > 10) datePickerView.dayOfMonth.toString() else "0" + datePickerView.dayOfMonth
            var time =
                datePickerView.year.toString() + "-" + month + "-" + day + " " + h + ":" + m
            callback.onSubmitTime(time)
        }
    }


    companion object {
        interface OnSubmitTimeCallback {
            fun onSubmitTime(time: String)
        }
    }


    fun setOnSubmitTimeCallback(c: OnSubmitTimeCallback) {
        this.callback = c
    }
}